package action.star;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Page;
import model.Star;
import model.StarHistory;
import model.User;

import DAO.StarDAO;
import DAO.DAOImple.StarImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StarAction extends ActionSupport {
	private List<User> memberList;
	private Star star;
	private List<Star> stars;
	private String year;
	private String qishu;
	private Page page;

	private Map<String, List<User>> memList;
	
	private Map<String, StarHistory> starHistoryList;

	public String execute() {
		return SUCCESS;
	}

	public String getNewStar() {
		StarDAO starDao = new StarImple();
		stars = starDao.getNewStars(qishu);
		return SUCCESS;
	}

	public String getStarHistory() {
		StarDAO starDao = new StarImple();
		
		page = new Page();
		page.setCurrentPage(1);
		
		page = page.getPageDistinct("SELECT count(distinct qishu) FROM T_Stars",page.getCurrentPage(),50);
		
		starHistoryList = starDao.getStarHistory();
		return SUCCESS;
	}

	public String openStar() {
		StarDAO starDao = new StarImple();
		int result = starDao.openStar(year + qishu);
		if (result != 0) {
			ActionContext act = ActionContext.getContext();
			int userGrpupId = ((User) act.getSession().get("user")).getGroup()
					.getId();
			star = starDao.getQishu(userGrpupId);
			memberList = starDao.getMemberList(userGrpupId);
		}
		return SUCCESS;
	}

	public String getStarAddInfo() {
		ActionContext act = ActionContext.getContext();
		int userGrpupId = ((User) act.getSession().get("user")).getGroup()
				.getId();
		StarDAO starDao = new StarImple();
		star = starDao.getQishu(userGrpupId);
		if (star != null)
			memberList = starDao.getMemberList(userGrpupId);

		return SUCCESS;
	}

	public String addStar() {
		ActionContext act = ActionContext.getContext();
		int userGrpupId = ((User) act.getSession().get("user")).getGroup()
				.getId();
		StarDAO starDao = new StarImple();
		if (star.getUser() == null) {
			act.put("addwrong", "0");
			memberList = starDao.getMemberList(userGrpupId);
			return ERROR;
		} else if (star.getBookName().trim().equals("")) {
			act.put("addwrong", "1");
			memberList = starDao.getMemberList(userGrpupId);
			return ERROR;
		} else if (star.getDescription().trim().equals("")) {
			act.put("addwrong", "2");
			memberList = starDao.getMemberList(userGrpupId);
			return ERROR;
		} else {
			int result = starDao.addStar(star);
			if (result != 0) {
				starHistoryList = starDao.getStarHistory();
			} else {
				act.put("addwrong", "3");
				memberList = starDao.getMemberList(userGrpupId);
				return ERROR;
			}
		}
		return SUCCESS;
	}

	public String modifyStar() {
		StarDAO starDao = new StarImple();
		ActionContext act = ActionContext.getContext();
		if(star != null) {
			star.setQishu(qishu);
			if (star.getUser() == null) {
				act.put("addwrong", "0");   //用户名为空
			} else if (star.getBookName().trim().equals("")) {
				act.put("addwrong", "1");  //书籍名称为空
			} else if (star.getDescription().trim().equals("")) {
				act.put("addwrong", "2");  //组长评语为空
			}else {
				int result = starDao.modifyStar(star);
				if(result != 0){
					act.put("addwrong", "4");  //修改成功
				}else {
					act.put("addwrong", "3");  //修改失败
				}
			}
		}
		stars = starDao.getNewStars(qishu);
		memList = new HashMap<String, List<User>>();
		memList.put("javaMember", starDao.getMemberList(2));
		memList.put("phpMember", starDao.getMemberList(3));
		return SUCCESS;
	}
	
	public String deleteStar(){
		StarDAO starDao = new StarImple();
		int result = starDao.deleteStar(qishu);
		starHistoryList = starDao.getStarHistory();
		return SUCCESS;
	}
	
	public List<User> getMemberList() {
		return memberList;
	}

	public void setMemberList(List<User> memberList) {
		this.memberList = memberList;
	}

	public Star getStar() {
		return star;
	}

	public void setStar(Star star) {
		this.star = star;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getQishu() {
		return qishu;
	}

	public void setQishu(String qishu) {
		this.qishu = qishu;
	}

	public List<Star> getStars() {
		return stars;
	}

	public void setStars(List<Star> stars) {
		this.stars = stars;
	}

	public Map<String, StarHistory> getStarHistoryList() {
		return starHistoryList;
	}

	public void setStarHistoryList(Map<String, StarHistory> starHistoryList) {
		this.starHistoryList = starHistoryList;
	}

	public Map<String, List<User>> getMemList() {
		return memList;
	}

	public void setMemList(Map<String, List<User>> memList) {
		this.memList = memList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}
